#include "piece_soldier.hpp"

bool Piece_Soldier::movex(int detx) {
    if (color == RED) {
        if (detx < 0) {
            if (pieces[gameMap[coor.x - 1][coor.y]]->isLife()) pieces[gameMap[coor.x - 1][coor.y]]->setLife(false);
            updateCoor(gameMap, Coordinate(coor.x - 1, coor.y));
            return true;
        } else return false;
    }
    if (detx > 0) {
        if (pieces[gameMap[Coordinate(coor.x + 1, coor.y)]]->isLife())
            pieces[gameMap[Coordinate(coor.x + 1, coor.y)]]->setLife(false);
        updateCoor(gameMap, Coordinate(coor.x + 1, coor.y));
        return true;
    }
    return false;
}

bool Piece_Soldier::movey(const Coordinate coor_end) {
    if (color == RED) {
        if (coor.x <= 4) {
            if (pieces[gameMap[coor_end]]->isLife()) pieces[gameMap[coor_end]]->setLife(false);
            updateCoor(gameMap, coor_end);
            return true;
        }
        return false;
    }
    if (coor.x >= 5) {
        if (pieces[gameMap[coor_end]]->isLife()) pieces[gameMap[coor_end]]->setLife(false);
        updateCoor(gameMap, coor_end);
        return true;
    }
    return false;
}

bool Piece_Soldier::move(const Coordinate &coor_end) {
    Coordinate det = coor_end - coor;
    if (abs(det.x) + abs(det.y) == 1) {
        if (abs(det.x))return movex(det.x);
        else {
            return movey(coor_end);
        }
    }
    return false;
}

void Piece_Soldier::init() {
    switch (pt) {
        case PieceType::RED_SOLDIER1:
            coor.x = 6;
            coor.y = 0;
            color = RED;
            break;
        case PieceType::RED_SOLDIER2:
            coor.x = 6;
            coor.y = 2;
            color = RED;
            break;
        case PieceType::RED_SOLDIER3:
            coor.x = 6;
            coor.y = 4;
            color = RED;
            break;
        case PieceType::RED_SOLDIER4:
            coor.x = 6;
            coor.y = 6;
            color = RED;
            break;
        case PieceType::RED_SOLDIER5:
            coor.x = 6;
            coor.y = 8;
            color = RED;
            break;
        case PieceType::BLACK_SOLDIER1:
            coor.x = 3;
            coor.y = 0;
            color = BLACK;
            break;
        case PieceType::BLACK_SOLDIER2:
            coor.x = 3;
            coor.y = 2;
            color = BLACK;
            break;
        case PieceType::BLACK_SOLDIER3:
            coor.x = 3;
            coor.y = 4;
            color = BLACK;
            break;
        case PieceType::BLACK_SOLDIER4:
            coor.x = 3;
            coor.y = 6;
            color = BLACK;
            break;
        case PieceType::BLACK_SOLDIER5:
            coor.x = 3;
            coor.y = 8;
            color = BLACK;
            break;
        default:
            break;
    }
    life = true;
}

Piece_Soldier::~Piece_Soldier() noexcept {}
